#ifndef OPUS_ENCODER_H
#define OPUS_ENCODER_H

#include <stdio.h>
#include <ogg/ogg.h>
#include <opus.h>

#define SAMPLE_RATE 8000//12000

#if (SAMPLE_RATE==8000)
#define FRMAE_SIZE 320
#elif (SAMPLE_RATE==12000)
#define FRMAE_SIZE 480
#elif (SAMPLE_RATE==16000)
#define FRMAE_SIZE 320
#endif

typedef int(*FUN_OGG_OUT)(unsigned char *buf, int len, void *priv);

typedef struct StOpusEnc
{
    int is_init;
    OpusEncoder* encoder;
    ogg_stream_state oggState;
    int frame_size;
    unsigned char *tmpOutBuf;
    int szTmpOutBuf;
    int nCntsFrame;
    unsigned short *tmpInBuf;
    int szTmpInBuf;
    int nPktWrted;
    ogg_packet* oggHP0;
    ogg_packet* oggHP1;
    int isOggHead;//ogg head
    FUN_OGG_OUT fun_out;
    ogg_int64_t granulepos_factor;
    void *priv;
} StOpusEnc;


enum {
    OPUS_PRIV_START = 0,
    OPUS_PRIV_NEXT,
};

typedef struct StOpusEncPriv {
    FUN_OGG_OUT cb_fun;
    char *url;
    char *opus_path;
    FILE *fo;
    int status; //0:start; 1:go on; 2: ended
    int total;
    int samplerate;
    int frame_size;
    int nChans;
    unsigned char *pcm_bytes;
    int pos;
    struct StOpusEnc* pOpusEnc;
}StOpusEncPriv;


struct StOpusEnc* OpusEncoderCreate(int fs, int nChans, FUN_OGG_OUT *fun_out);
int OpusEncoderWrite(struct StOpusEnc *enc, int nChans, int nFrames,
    unsigned short* ibuf, int is_eos, int *nWrted);
int OpusEncoderDelete(struct StOpusEnc *enc);

int OpusEncoderFile(char *wav_path, char *opus_path);

int OpusEncoderPcmData(unsigned char *data, int len, int is_end, int CPU_Reset, void *priv);

#endif
